﻿# -*- coding: utf-8 -*-
import sys  
reload(sys)
sys.setdefaultencoding('utf8')  
import string
import cgi
import urllib
import datetime
import random

from sgmllib import SGMLParser, SGMLParseError
from google.appengine.ext import db
from google.appengine.api import urlfetch
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from tweepy import *

consumer_key = "fcbxGwlLaBQXTfDak5FnA"
consumer_secret = "RDSQ2Kw2WmZh6XbWZaNKavYLrP2ZY6C55YFmKoqE1E"
oauth_token = " "
oauth_verifier = " "

StartUrl = "/API/"
SearchUrl = "/API/search.json"
TwitterUrl = "https://api.twitter.com/1.1/"
TwitterSearchUrl = "http://search.twitter.com/"

TLGetStartUrl="/API/statuses/"
TLMentionsOldUrl="/API/statuses/mentions"
TLMentionsUrl="/API/statuses/mentions_timeline"
TLUserUrl="/API/statuses/user_timeline"
TLHomeUrl="/API/statuses/home_timeline"
TLRetweetsOfMeUrl="/API/statuses/retweets_of_me"

UsersStartUrl="/API/users"
UsersShowUrl="/API/users/show"
UsersSearchUrl="/API/users/search"

ListsStartUrl="/API/lists"
ListsListUrl="/API/lists/list"
ListsStatusesUrl="/API/lists/statuses"
ListsShowUrl="/API/lists/show"

DMGetStartUrl = "/API/direct_messages"
DMGetSentUrl = "/API/direct_messages/sent"
DMGetSpecifiedUrl = "/API/direct_messages/"

TweetsUpdateUrl = "/statuses/update"
RetweetUrl = "/statuses/retweet/"
DelTweetUrl = "/statuses/destroy/"
PostDMUrl = "/direct_messages/new"
DelDMUrl = "/direct_messages/destroy/"
FriendCreateUrl = "/friendships/create"
FriendDelUrl = "/friendships/destroy"
FriendUpdateUrl = "/friendships/update"
FavCreateUrl = "/favorites/create/"
FavDelUrl = "/favorites/destroy/"
ListMemberDelUrl = "/lists/members/destroy"
ListMemberAddUrl = "/lists/members/create"
ListDelUrl = "/lists/destroy"
ListCreateUrl = "/lists/create"
ListUpdateUrl = "/lists/update"
ListSubCreate = "/lists/subscribers/create"
ListSubDel = "/lists/subscribers/destroy"
SearchCreateUrl = "/saved_searches/create"
SearchDelUrl = "/saved_searches/destroy/"

class UserToken(db.Model):
	username = db.StringProperty(required=True)
	usernumber = db.StringProperty(required=True)
	oauth_token = db.StringProperty(required=True)
	oauth_verifier = db.StringProperty(required=True)
	access_token_key = db.StringProperty(required=True)
	access_token_secret = db.StringProperty(required=True)
	consumer_key = db.StringProperty(required=False)
	consumer_secret = db.StringProperty(required=False)
	createdtime = db.DateTimeProperty(required=True, auto_now_add=True)

class TwitterOauth(webapp.RequestHandler):
	def __init__(self):
		self.apikey = consumer_key
		self.apisecret = consumer_secret
		self.try_error = False
		self.try_times = 5
	def testoauth(self, callback, url = None):
		if callback == True:
			auth = OAuthHandler(self.apikey, self.apisecret, url)
			auth_url = auth.get_authorization_url()
			self.response.out.write(auth_url)
		else:
			auth = OAuthHandler(consumer_key, consumer_secret)
			auth_url = auth.get_authorization_url()
			self.redirect(auth_url)
	def get(self, callurl):
		if callurl.startswith(TLGetStartUrl):
			access_token = cgi.escape(self.request.get("oauth_token"))
			usernum = access_token[0:access_token.index("-")]
			if usernum != None or usernum != "":
				UserObjGQL = UserToken.gql("WHERE usernumber = :1", usernum)
				UserObj = UserObjGQL.get()
				if UserObj != None:
					self.apikey = UserObj.consumer_key
					self.apisecret = UserObj.consumer_secret
					auth = OAuthHandler(self.apikey, self.apisecret, None, True)
					auth.set_access_token(UserObj.access_token_key.decode("utf-8"), UserObj.access_token_secret.decode("utf-8"))
					api = API(auth, 'api.twitter.com', 'search.twitter.com', None, True)
				else:
					self.response.out.write("User:"+ usernum + "Not Oauth yet!<br />")
					return
			if callurl.startswith(TLHomeUrl):
				since_id = cgi.escape(self.request.get("since_id"))
				if since_id == "":
					since_id = None
				max_id = cgi.escape(self.request.get("max_id"))
				if max_id == "":
					max_id = None
				countnum = cgi.escape(self.request.get("count"))
				if countnum == "":
					countnum = "100"
				trim_user = cgi.escape(self.request.get("trim_user"))
				if trim_user == "":
					trim_user = None
				exclude_replies = cgi.escape(self.request.get("exclude_replies"))
				if exclude_replies == "":
					exclude_replies = None
				include_entities = cgi.escape(self.request.get("include_entities"))
				if include_entities == "":
					include_entities = None
				contributor_details = cgi.escape(self.request.get("contributor_details"))
				if contributor_details == "":
					contributor_details = None
				(s,resps) = api.home_timeline(since_id, max_id, countnum, trim_user, exclude_replies, contributor_details, include_entities)
				self.response.out.write(resps)
				return
			if callurl.startswith(TLMentionsUrl) or callurl.startswith(TLMentionsOldUrl):
				since_id = cgi.escape(self.request.get("since_id"))
				if since_id == "":
					since_id = None
				max_id = cgi.escape(self.request.get("max_id"))
				if max_id == "":
					max_id = None
				countnum = cgi.escape(self.request.get("count"))
				if countnum == "":
					countnum = "100"
				trim_user = cgi.escape(self.request.get("trim_user"))
				if trim_user == "":
					trim_user = None
				include_entities = cgi.escape(self.request.get("include_entities"))
				if include_entities == "":
					include_entities = None
				contributor_details = cgi.escape(self.request.get("contributor_details"))
				if contributor_details == "":
					contributor_details = None
				(s,resps) = api.mentions(since_id, max_id, countnum, trim_user, contributor_details, include_entities)
				self.response.out.write(resps)
				return
			if callurl.startswith(TLRetweetsOfMeUrl):
				since_id = cgi.escape(self.request.get("since_id"))
				if since_id == "":
					since_id = None
				max_id = cgi.escape(self.request.get("max_id"))
				if max_id == "":
					max_id = None
				countnum = cgi.escape(self.request.get("count"))
				if countnum == "":
					countnum = "100"
				trim_user = cgi.escape(self.request.get("trim_user"))
				if trim_user == "":
					trim_user = None
				include_entities = cgi.escape(self.request.get("include_entities"))
				if include_entities == "":
					include_entities = None
				include_user_entities  = cgi.escape(self.request.get("include_user_entities"))
				if include_user_entities  == "":
					include_user_entities  = None
				(s,resps) = api.retweets_of_me(since_id, max_id, countnum, trim_user, include_entities, include_user_entities)
				self.response.out.write(resps)
				return
			if callurl.startswith(TLUserUrl):
				user_id = cgi.escape(self.request.get("user_id"))
				if user_id == "" or user_id == None:
					user_id = usernum
				screen_name = cgi.escape(self.request.get("screen_name"))
				if screen_name == "":
					screen_name = None
				since_id = cgi.escape(self.request.get("since_id"))
				if since_id == "":
					since_id = None
				max_id = cgi.escape(self.request.get("max_id"))
				if max_id == "":
					max_id = None
				countnum = cgi.escape(self.request.get("count"))
				if countnum == "":
					countnum = "100"
				trim_user = cgi.escape(self.request.get("trim_user"))
				if trim_user == "":
					trim_user = None
				exclude_replies = cgi.escape(self.request.get("exclude_replies"))
				if exclude_replies == "":
					exclude_replies = None
				include_rts = cgi.escape(self.request.get("include_rts"))
				if include_rts == "":
					include_rts = None
				contributor_details = cgi.escape(self.request.get("contributor_details"))
				if contributor_details == "":
					contributor_details = None
				(s,resps) = api.user_timeline(user_id, screen_name, since_id, max_id, countnum, trim_user, exclude_replies, include_rts, contributor_details)
				self.response.out.write(resps)
				return
		if callurl.startswith(UsersStartUrl):
			access_token = cgi.escape(self.request.get("oauth_token"))
			usernum = access_token[0:access_token.index("-")]
			if usernum != None or usernum != "":
				UserObjGQL = UserToken.gql("WHERE usernumber = :1", usernum)
				UserObj = UserObjGQL.get()
				if UserObj != None:
					self.apikey = UserObj.consumer_key
					self.apisecret = UserObj.consumer_secret
					auth = OAuthHandler(self.apikey, self.apisecret, None, True)
					auth.set_access_token(UserObj.access_token_key.decode("utf-8"), UserObj.access_token_secret.decode("utf-8"))
					api = API(auth, 'api.twitter.com', 'search.twitter.com', None, True)
				else:
					self.response.out.write("User:"+ usernum + "Not Oauth yet!<br />")
					return
			if callurl.startswith(UsersShowUrl):
				user_id = cgi.escape(self.request.get("user_id"))
				if user_id == "":
					user_id = None
				screen_name = cgi.escape(self.request.get("screen_name"))
				if screen_name == "":
					screen_name = None
				include_entities = cgi.escape(self.request.get("include_entities"))
				if include_entities == "":
					include_entities = None
				(s,resps) = api.get_user(user_id, screen_name, include_entities)
				self.response.out.write(resps)
				return
			if callurl.startswith(UsersSearchUrl):
				q = cgi.escape(self.request.get("q"))
				if q == "":
					q = None
				page = cgi.escape(self.request.get("page"))
				if page == "":
					page = None
				countnum = cgi.escape(self.request.get("count"))
				if countnum == "":
					countnum = "100"
				include_entities = cgi.escape(self.request.get("include_entities"))
				if include_entities == "":
					include_entities = None
				(s,resps) = api.search_users(q, page, countnum, include_entities)
				self.response.out.write(resps)
				return
		if callurl.startswith(ListsStartUrl):
			access_token = cgi.escape(self.request.get("oauth_token"))
			usernum = access_token[0:access_token.index("-")]
			if usernum != None or usernum != "":
				UserObjGQL = UserToken.gql("WHERE usernumber = :1", usernum)
				UserObj = UserObjGQL.get()
				if UserObj != None:
					self.apikey = UserObj.consumer_key
					self.apisecret = UserObj.consumer_secret
					auth = OAuthHandler(self.apikey, self.apisecret, None, True)
					auth.set_access_token(UserObj.access_token_key.decode("utf-8"), UserObj.access_token_secret.decode("utf-8"))
					api = API(auth, 'api.twitter.com', 'search.twitter.com', None, True)
				else:
					self.response.out.write("User:"+ usernum + "Not Oauth yet!<br />")
					return
			if callurl.startswith(ListsListUrl):
				user_id = cgi.escape(self.request.get("user_id"))
				if user_id == "":
					user_id = None
				screen_name = cgi.escape(self.request.get("screen_name"))
				if screen_name == "":
					screen_name = None
				(s,resps) = api.lists_list_new(user_id, screen_name)
				self.response.out.write(resps)
				return
			if callurl.startswith(ListsStatusesUrl):
				list_id = cgi.escape(self.request.get("list_id"))
				if list_id == "":
					list_id = None
				slug = cgi.escape(self.request.get("slug"))
				if slug == "":
					slug = None
				owner_screen_name = cgi.escape(self.request.get("owner_screen_name"))
				if owner_screen_name == "":
					owner_screen_name = None
				owner_id = cgi.escape(self.request.get("owner_id"))
				if owner_id == "":
					owner_id = None
				since_id = cgi.escape(self.request.get("since_id"))
				if since_id == "":
					since_id = None
				max_id = cgi.escape(self.request.get("max_id"))
				if max_id == "":
					max_id = None
				countnum = cgi.escape(self.request.get("count"))
				if countnum == "":
					countnum = None
				include_entities = cgi.escape(self.request.get("include_entities"))
				if include_entities == "":
					include_entities = None
				include_rts = cgi.escape(self.request.get("include_rts"))
				if include_rts == "":
					include_rts = None
				(s,resps) = api.lists_statuses_new(list_id, slug, owner_screen_name, owner_id, since_id, max_id, countnum, include_entities, include_rts)
				self.response.out.write(resps)
				return
			if callurl.startswith(ListsShowUrl):
				list_id = cgi.escape(self.request.get("list_id"))
				if list_id == "":
					list_id = None
				slug = cgi.escape(self.request.get("slug"))
				if slug == "":
					slug = None
				owner_screen_name = cgi.escape(self.request.get("owner_screen_name"))
				if owner_screen_name == "":
					owner_screen_name = None
				owner_id = cgi.escape(self.request.get("owner_id"))
				if owner_id == "":
					owner_id = None
				(s,resps) = api.lists_show_new(list_id, slug, owner_screen_name, owner_id)
				self.response.out.write(resps)
				return
		if callurl.startswith(DMGetStartUrl):
			access_token = cgi.escape(self.request.get("oauth_token"))
			usernum = access_token[0:access_token.index("-")]
			if usernum != None or usernum != "":
				UserObjGQL = UserToken.gql("WHERE usernumber = :1", usernum)
				UserObj = UserObjGQL.get()
				if UserObj != None:
					self.apikey = UserObj.consumer_key
					self.apisecret = UserObj.consumer_secret
					auth = OAuthHandler(self.apikey, self.apisecret, None, True)
					auth.set_access_token(UserObj.access_token_key.decode("utf-8"), UserObj.access_token_secret.decode("utf-8"))
					api = API(auth, 'api.twitter.com', 'search.twitter.com', None, True)
				else:
					self.response.out.write("User:"+ usernum + "Not Oauth yet!<br />")
					return
			if callurl.startswith(DMGetSentUrl):
				since_id = cgi.escape(self.request.get("since_id"))
				if since_id == "":
					since_id = None
				max_id = cgi.escape(self.request.get("max_id"))
				if max_id == "":
					max_id = None
				countnum = cgi.escape(self.request.get("count"))
				if countnum == "":
					countnum = "100"
				page = cgi.escape(self.request.get("page"))
				if page == "":
					page = None
				include_entities = cgi.escape(self.request.get("include_entities"))
				if include_entities == "":
					include_entities = None
				(s,resps) = api.sent_direct_messages(since_id, max_id, countnum, page, include_entities)
				self.response.out.write(resps)
				return
			elif callurl.startswith(DMGetSpecifiedUrl):
				msgid = callurl[len(DMGetSpecifiedUrl):-5]
				#(s,resps) = api.get_direct_message_new(msgid)
				self.response.out.write(resps)
				return
			else:
				since_id = cgi.escape(self.request.get("since_id"))
				if since_id == "":
					since_id = None
				max_id = cgi.escape(self.request.get("max_id"))
				if max_id == "":
					max_id = None
				countnum = cgi.escape(self.request.get("count"))
				if countnum == "":
					countnum = "100"
				page = cgi.escape(self.request.get("page"))
				if page == "":
					page = None
				include_entities = cgi.escape(self.request.get("include_entities"))
				if include_entities == "":
					include_entities = None
				skip_status = cgi.escape(self.request.get("skip_status"))
				if skip_status == "":
					skip_status = None
				(s,resps) = api.direct_messages(since_id, max_id, countnum, page, skip_status, include_entities)
				self.response.out.write(resps)
				return
		if callurl.startswith(StartUrl):
			if callurl.startswith(SearchUrl):
				ReUrl = TwitterSearchUrl + callurl[len(StartUrl):] + "?" + self.request.query_string
				result = urlfetch.fetch(url=ReUrl, payload=[], method=urlfetch.GET, headers={'Host':'search.twitter.com', 'Content-Type': 'application/x-www-form-urlencoded','User-Agent':'Gravity/1.61 (Build 6821) 3gpp-gba', 'Authorization':'', 'X-Forwarded-For':''},follow_redirects = True, allow_truncated=True, deadline=30)
			else:
				ReUrl = TwitterUrl + callurl[len(StartUrl):] + "?" + self.request.query_string
				result = urlfetch.fetch(url=ReUrl, payload=[], method=urlfetch.GET, headers={'Host':'api.twitter.com', 'Content-Type': 'application/x-www-form-urlencoded','User-Agent':'Gravity/1.61 (Build 6821) 3gpp-gba', 'Authorization':'', 'X-Forwarded-For':'', 'Expect':''},follow_redirects = True, allow_truncated=True, deadline=30)
			self.response.out.write(result.content)
			return
		if cgi.escape(self.request.get('oauth_token')) != "":
			oauth_token = cgi.escape(self.request.get('oauth_token'))
			self.response.out.write(oauth_token + "<br />")
		else:
			oauth_token = " "
		if cgi.escape(self.request.get('oauth_verifier')) != "":
			oauth_verifier = cgi.escape(self.request.get('oauth_verifier'))
			self.response.out.write(oauth_verifier + "<br />")
		else:
			oauth_verifier = " "
		if oauth_token == " " or oauth_verifier == " ":
			key = cgi.escape(self.request.get('consumer_key'))
			secret = cgi.escape(self.request.get('consumer_secret'))
			if len(key) > 0 and len(secret) > 0:
				self.apikey = key
				self.apisecret = secret
				self.testoauth(True, cgi.escape(self.request.get('callback')))
				return
			else:
				self.testoauth(False)
		else:
			key = cgi.escape(self.request.get('consumer_key'))
			secret = cgi.escape(self.request.get('consumer_secret'))
			if len(key) > 0 and len(secret) > 0:
				self.apikey = key
				self.apisecret = secret
			auth = OAuthHandler(self.apikey, self.apisecret)
			auth.set_request_token(oauth_token, oauth_verifier)
			access_token = auth.get_access_token(oauth_verifier)
			usernum = access_token.key[0:access_token.key.index("-")]
			UserObjGQL = UserToken.gql("WHERE usernumber = :1", usernum)
			UserObj = UserObjGQL.get()
			if UserObj != None:
				UserObj.delete()
			UserObj = UserToken(username = auth.get_username(), usernumber = usernum, oauth_token = oauth_token, oauth_verifier = oauth_verifier, access_token_key = access_token.key, access_token_secret = access_token.secret, consumer_key = self.apikey, consumer_secret = self.apisecret)
			UserObj.put()
			api = API(auth)
			username = auth.get_username()
			if username == 'flamehe' or username == 'sbgfw' or username == 'CMTDLTE':
				#s = api.update_status('Oauth Success! @flamehe %i' % random.randint(0, 1000))
				self.response.out.write("Oauth Success!<br />")
				return
			elif username == 'LingJihua' or username == 'zhanggaoli':
				#s = api.update_status('中南海网络测试 %i' % random.randint(0, 1000))
				self.response.out.write("Oauth Success!<br />")
				return
			else:
				s = api.update_status('Oauth Success! @flamehe %i' % random.randint(0, 1000))
			self.response.out.write("Oauth Success!<br />")
			return
	def post(self, callurl):
		access_token = cgi.escape(self.request.get("oauth_token"))
		usernum = access_token[0:access_token.index("-")]
		if usernum != None or usernum != "":
			UserObjGQL = UserToken.gql("WHERE usernumber = :1", usernum)
			UserObj = UserObjGQL.get()
			if UserObj != None:
				self.apikey = UserObj.consumer_key
				self.apisecret = UserObj.consumer_secret
				auth = OAuthHandler(self.apikey, self.apisecret)
				auth.set_access_token(UserObj.access_token_key.decode("utf-8"), UserObj.access_token_secret.decode("utf-8"))
				api = API(auth, 'api.twitter.com', 'search.twitter.com', None, True)
			else:
				self.response.out.write("User:"+ usernum + "Not Oauth yet!<br />")
				return
		else:
			self.response.out.write("Wrong post, must have access_token<br />")
			return
		if callurl.startswith(TweetsUpdateUrl):
			statuscontent = cgi.escape(self.request.get("status"))
			statuscontent = statuscontent[1:]
			in_reply_to_status_id = cgi.escape(self.request.get("in_reply_to_status_id"))
			geo_lat = cgi.escape(self.request.get("lat"))
			geo_long = cgi.escape(self.request.get("long"))
			place_id = cgi.escape(self.request.get("place_id"))
			source = cgi.escape(self.request.get("source"))
			#display_coordinates = cgi.escape(self.request.get("display_coordinates"))
			#trim_user = cgi.escape(self.request.get("trim_user"))
			#include_entities = cgi.escape(self.request.get("include_entities"))
			#postcontentlist = self.request.arguments()
			#for key in postcontentlist:
			#content = cgi.escape(self.request.get(key))
			#self.response.out.write(key +":"+ content + "<br />")
			(s,resps) = api.update_status(statuscontent, in_reply_to_status_id, geo_lat, geo_long, source, place_id)
			self.response.out.write(resps)
			return
		elif callurl.startswith(RetweetUrl):
			msgid = callurl[len(RetweetUrl):-5]
			#trim_user = cgi.escape(self.request.get("trim_user"))
			#include_entities = cgi.escape(self.request.get("include_entities"))
			(s,resps) = api.retweet(msgid)
			self.response.out.write(resps)
			return
		elif callurl.startswith(DelTweetUrl):
			msgid = callurl[len(DelTweetUrl):-5]
			#msgid = cgi.escape(self.request.get("id"))
			#trim_user = cgi.escape(self.request.get("trim_user"))
			#include_entities = cgi.escape(self.request.get("include_entities"))
			(s,resps) = api.destroy_status(msgid)
			self.response.out.write(resps)
			return
		elif callurl.startswith(PostDMUrl):
			#screen_name = cgi.escape(self.request.get("screen_name"))
			#userid = cgi.escape(self.request.get("userid"))
			userget = cgi.escape(self.request.get("user"))# Special for gravity
			if userget.isdigit():
				userid = userget
				#screen_name = None
			else:
				(u,respsu) = api.get_user(userget)
				userid = str(u.id)
				#screen_name = None
			text = cgi.escape(self.request.get("text"))
			(s,resps) = api.send_direct_message(None, None, userid, text)
			self.response.out.write(resps)
			return
		elif callurl.startswith(DelDMUrl):
			msgid = callurl[len(DelDMUrl):-5]
			#msgid = cgi.escape(self.request.get("id"))
			#include_entities = cgi.escape(self.request.get("include_entities"))
			(s,resps) = api.destroy_direct_message(msgid)
			self.response.out.write(resps)
			return
		elif callurl.startswith(FriendCreateUrl):
			userid = cgi.escape(self.request.get("user"))
			screen_name = cgi.escape(self.request.get("screen_name"))
			follow = cgi.escape(self.request.get("follow"))
			(s,resps) = api.create_friendship(None, userid, screen_name, follow)
			self.response.out.write(resps)
			return
		elif callurl.startswith(FriendDelUrl):
			userid = cgi.escape(self.request.get("user"))
			screen_name = cgi.escape(self.request.get("screen_name"))
			#include_entities = cgi.escape(self.request.get("include_entities"))
			(s,resps) = api.destroy_friendship(None, userid, screen_name)
			self.response.out.write(resps)
			return
		elif callurl.startswith(FriendUpdateUrl):
			return
			userid = cgi.escape(self.request.get("user"))
			#screen_name = cgi.escape(self.request.get("screen_name"))
			screen_name = None
			device = cgi.escape(self.request.get("device"))
			retweets  = cgi.escape(self.request.get("retweets"))
			#(s,resps) = api not Implemented
			self.response.out.write(resps)
		elif callurl.startswith(FavCreateUrl):
			msgid = callurl[len(FavCreateUrl):-5]
			#include_entities = cgi.escape(self.request.get("include_entities"))
			(s,resps) = api.create_favorite(msgid)
			self.response.out.write(resps)
			return
		elif callurl.startswith(FavDelUrl):
			msgid = callurl[len(FavDelUrl):-5]
			#include_entities = cgi.escape(self.request.get("include_entities"))
			(s,resps) = api.destroy_favorite(msgid)
			self.response.out.write(resps)
			return
		elif callurl.startswith(ListMemberDelUrl):
			list_id = cgi.escape(self.request.get("list_id"))
			slug = cgi.escape(self.request.get("slug"))
			user_id = cgi.escape(self.request.get("user_id"))
			screen_name = cgi.escape(self.request.get("screen_name"))
			owner_screen_name = cgi.escape(self.request.get("owner_screen_name"))
			owner_id = cgi.escape(self.request.get("owner_id"))
			(s,resps) = api.remove_list_member_new(list_id, slug, user_id, screen_name, owner_screen_name, owner_id)
			self.response.out.write(resps)
			return
		elif callurl.startswith(ListMemberAddUrl):
			list_id = cgi.escape(self.request.get("list_id"))
			slug = cgi.escape(self.request.get("slug"))
			user_id = cgi.escape(self.request.get("user_id"))
			screen_name = cgi.escape(self.request.get("screen_name"))
			owner_screen_name = cgi.escape(self.request.get("owner_screen_name"))
			owner_id = cgi.escape(self.request.get("owner_id"))
			(s,resps) = api.add_list_member_new(list_id, slug, user_id, screen_name, owner_screen_name, owner_id)
			self.response.out.write(resps)
			return
		elif callurl.startswith(ListDelUrl):
			list_id = cgi.escape(self.request.get("list_id"))
			slug = cgi.escape(self.request.get("slug"))
			owner_screen_name = cgi.escape(self.request.get("owner_screen_name"))
			owner_id = cgi.escape(self.request.get("owner_id"))
			(s,resps) = api.destroy_list_new(owner_screen_name, owner_id, list_id, slug)
			self.response.out.write(resps)
			return
		elif callurl.startswith(ListCreateUrl):
			name = cgi.escape(self.request.get("name"))
			mode = cgi.escape(self.request.get("mode"))
			description = cgi.escape(self.request.get("description"))
			(s,resps) = api.create_list_new(name, mode, description)
			self.response.out.write(resps)
			return
		elif callurl.startswith(ListUpdateUrl):
			list_id = cgi.escape(self.request.get("list_id"))
			slug = cgi.escape(self.request.get("slug"))
			owner_screen_name = cgi.escape(self.request.get("owner_screen_name"))
			owner_id = cgi.escape(self.request.get("owner_id"))
			name = cgi.escape(self.request.get("name"))
			mode = cgi.escape(self.request.get("mode"))
			description = cgi.escape(self.request.get("description"))
			(s,resps) = api.update_list_new(owner_screen_name, owner_id, list_id, slug, name, mode, description)
			self.response.out.write(resps)
			return
		elif callurl.startswith(ListSubCreate):
			list_id = cgi.escape(self.request.get("list_id"))
			slug = cgi.escape(self.request.get("slug"))
			owner_screen_name = cgi.escape(self.request.get("owner_screen_name"))
			owner_id = cgi.escape(self.request.get("owner_id"))
			(s,resps) = api.subscribe_list_new(owner_screen_name, owner_id, list_id, slug)
			self.response.out.write(resps)
			return
		elif callurl.startswith(ListSubDel):
			list_id = cgi.escape(self.request.get("list_id"))
			slug = cgi.escape(self.request.get("slug"))
			owner_screen_name = cgi.escape(self.request.get("owner_screen_name"))
			owner_id = cgi.escape(self.request.get("owner_id"))
			(s,resps) = api.unsubscribe_list_new(owner_screen_name, owner_id, list_id, slug)
			self.response.out.write(resps)
			return
		elif callurl.startswith(SearchCreateUrl):
			query = cgi.escape(self.request.get("query"))
			(s,resps) = api.create_saved_search(query)
			self.response.out.write(resps)
			return
		elif callurl.startswith(SearchDelUrl):
			searchid = callurl[len(SearchDelUrl):-5]
			(s,resps) = api.destroy_saved_search(searchid)
			self.response.out.write(resps)
			return
			
			
application = webapp.WSGIApplication(
																		 [(r'/twiauth(.*)', TwitterOauth)],
																		 debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()